package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealMapper.insert(setmeal);
        //主键回写
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }

        setmealDishMapper.insertBatch(setmealDishes);

    }

    @Override
    public PageResult queryPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> p =  setmealMapper.selectPage(setmealPageQueryDTO);
        long total = p.getTotal();
        List<SetmealVO> result = p.getResult();
        return new PageResult(total,result);
    }

    @Transactional
    @Override
    public void deleteSetmealAndSetmealDish(List<Long> ids) {
        //删除套餐表中的数据
        setmealMapper.deleteByIds(ids);
        //删除套餐菜品关系表中的数据
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        //获得套餐信息
        SetmealVO setmealVO = setmealMapper.getById(id);
        //获取关系菜品信息
        setmealVO.setSetmealDishes(setmealDishMapper.getInfoBySetmealId(id));
        return setmealVO;
    }

    @Transactional
    @Override
    public void updateSetmealAndSetmealDish(SetmealDTO setmealDTO) {
        //修改套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除重新添加新的关系菜品
        List<Long> list = new ArrayList<>();
        list.add(setmeal.getId());
        setmealDishMapper.deleteBySetmealIds(list);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.insertBatch(setmealDishes);

    }

    @Override
    public void startOrStopSetmealById(Integer status,Long id) {
        setmealMapper.updateStatusById(status,id);
    }

    @Override
    public List<Setmeal> getSetmealByCategoryId(Long categoryId) {
        return setmealMapper.getSetmealByCategoryId(categoryId);
    }



    @Override
    public List<DishItemVO> getSetmealDishBysetmealId(Long setmealId) {
        return setmealMapper.getSetmealDishBysetmealId(setmealId);
    }
}
